// 8085.cpp : Defines the class behaviors for the application.
//

/*
The intent of this is to state the conditions under which Packages
may be used, copied and distributed.

    "Package" refers to those files, including, but not limited to, source
    code, binary executables, images, and scripts, which are distributed by
    the author.

    "Author" refers to the person who wrote this program.

    "You" is you, if you're thinking about using, copying or distributing this     Package.

    The Author of the this program is Vijay Kumar.

    1.	You may not use this Package for commercial purposes.  

    2.	You may make and give away verbatim copies of this Package for personal use, 
		provided that you duplicate all of the original copyright notices and associated disclaimers.

    3.	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
		without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
		The author will not be responsible for any kind of loss occuring due to the use of this 
		package or the programs distributed with this.

    4.	This program is for educational purposes only and should not be distributed  or used for 
		commercial purposes.
*/

#include "stdafx.h"
#include "crystal/editcmd.h"
#include "8085.h"
#include "Label.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "8085Doc.h"
#include "8085View.h"
#include "Splash.h"
#include "GlobalFunctions.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int nDebugPrefix=0;
/////////////////////////////////////////////////////////////////////////////
// CMy8085App

BEGIN_MESSAGE_MAP(CMy8085App, CWinApp)
	//{{AFX_MSG_MAP(CMy8085App)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

BOOL CMy8085App::m_bIntelSDK=TRUE;
/////////////////////////////////////////////////////////////////////////////
// CMy8085App construction

CMy8085App::CMy8085App()
{
	m_szVersion ="1.7";

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMy8085App object

CMy8085App theApp;

/////////////////////////////////////////////////////////////////////////////
// CMy8085App initialization

BOOL CMy8085App::InitInstance()
{
	// CG: The following block was added by the Splash Screen component.
\
	{
\
		CCommandLineInfo cmdInfo;
\
		ParseCommandLine(cmdInfo);
\

\
		CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
\
	}
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	//Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings(8);  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_NONAMETYPE,
		// IDR_SAMPLETYPE,
		RUNTIME_CLASS(CMy8085Doc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CMy8085View));
	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	//	Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
	{
		return TRUE;
	}

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CLabel	m_CommercialLicense;
	CLabel	m_RegistrationMessage;
	CLabel	m_CopyRight;
	CLabel	m_VersionInfo;
	CLabel  m_ProductName;
	CLabel	m_MailTo;
	CLabel	m_HomePageURL;
	CString	m_szEmailID;
	CString	m_szHomePageURL;
	CLabel	m_PersonalHomePage;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	afx_msg void OnButtonOpenHomepage();
	afx_msg void OnButtonOpenEmail();
	virtual BOOL OnInitDialog();
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	m_szEmailID = _T("kumar__v@hotmail.com");
	m_szHomePageURL = _T("http://kumarv.tripod.com");
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_COMMERCIAL_LICENSE, m_CommercialLicense);
	DDX_Control(pDX, IDC_REGISTRATION_MESSAGE, m_RegistrationMessage);
	DDX_Control(pDX, IDC_COPYRIGHT, m_CopyRight);
	DDX_Control(pDX, IDC_VERSION_INFO, m_VersionInfo);
	DDX_Control(pDX, IDC_PRODUCT_NAME, m_ProductName);
	DDX_Control(pDX, IDC_MAILTO, m_MailTo);
	DDX_Control(pDX, IDC_HOMEPAGE, m_HomePageURL);
	DDX_Control(pDX, IDC_PERSONAL_HOMEPAGE, m_PersonalHomePage);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	// ON_BN_CLICKED(IDC_BUTTON_OPEN_HOMEPAGE, OnButtonOpenHomepage)
	// ON_BN_CLICKED(IDC_BUTTON_OPEN_EMAIL, OnButtonOpenEmail)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CMy8085App::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMy8085App commands
/*
BOOL CMy8085App::OnIdle(LONG lCount) 
{
	// TODO: Add your specialized code here and/or call the base class
    CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
    // Get the active MDI child window.
    CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
    // Get the active view attached to the active MDI child window.
	CStatusBar* pStatusBar = (CStatusBar*) pFrame->GetDescendantWindow(AFX_IDW_STATUS_BAR);

	if(pStatusBar)
	{
        if((CEditView*)pChild->GetActiveView())
        {
		    CEdit &edit = ((CEditView*)pChild->GetActiveView())->GetEditCtrl();
		    CString s1;
            UINT i = edit.LineFromChar();
            int nStart,nEnd;
            edit.GetSel(nStart,nEnd);
		    s1.Format(_T("Ln %05u, Col %05u"),i+1,nStart+1 - edit.LineIndex());
		    pStatusBar->SetPaneText(pStatusBar->CommandToIndex(ID_INDICATOR_LINE), s1);
        }
        else
        {
		    pStatusBar->SetPaneText(pStatusBar->CommandToIndex(ID_INDICATOR_LINE), "Vijay Kumar");
        }

	}
	return CWinApp::OnIdle(lCount);
}
*/
// True if intel sdk 
BOOL CMy8085App::IsIntelSDK()
{
    return m_bIntelSDK;
}

BOOL CMy8085App::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following lines were added by the Splash Screen component.
	if (CSplashWnd::PreTranslateAppMessage(pMsg))
		return TRUE;

	return CWinApp::PreTranslateMessage(pMsg);
}

BOOL CMy8085App::ProcessMessageFilter(int code, LPMSG lpMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

    if(VK_F1==lpMsg->wParam)
    {
        Help();
        return FALSE;
    }

    // this is for processing keyboard events for the C8085Dialog
    if(m_h8085Dialog!=NULL)
    {
        if ((lpMsg->hwnd == m_h8085Dialog) ||
            ::IsChild(m_h8085Dialog, lpMsg->hwnd))
            // Use ::IsChild to get messages that may be going
            // to the dialog's controls.  In the case of
            // WM_KEYDOWN this is required.
        {
            if (lpMsg->message == WM_KEYDOWN || lpMsg->message==WM_SYSKEYDOWN)
            {
                SendMessage(m_h8085Dialog,MY_WM_KEYDOWN,lpMsg->wParam,
                    lpMsg->lParam);
                    return FALSE;
            }
            else if(lpMsg->message == WM_KEYUP || lpMsg->message==WM_SYSKEYUP)
            {
                SendMessage(m_h8085Dialog,MY_WM_KEYUP,lpMsg->wParam,
                    lpMsg->lParam);
                    return FALSE;
            }
        }
    }
  	return CWinApp::ProcessMessageFilter(code, lpMsg);
}

void CAboutDlg::OnButtonOpenHomepage() 
{
	// TODO: Add your control notification handler code here
    if((UINT)::GotoURL(m_szHomePageURL,1)<=32)
        ::ShowErrorMessage("Could not invoke Internet browser");
}

void CAboutDlg::OnButtonOpenEmail() 
{
	// TODO: Add your control notification handler code here
	if((UINT)GotoURL("mailto:"+m_szEmailID,1)<=32)
			::ShowErrorMessage("Could not invoke mail program");
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	/*
		m_ProductName.SetFontBold( FALSE );
		m_AuthorName.SetFontBold( FALSE );
		m_RegistrationMessage.SetFontBold( FALSE );
	*/

	m_VersionInfo.SetText( "Version " + theApp.m_szVersion  + " ( UNREGISTERED )");

	m_HomePageURL.SetLink(TRUE)
		.SetTextColor(RGB(0,0,255))
		.SetLinkCursor(AfxGetApp()->LoadCursor(IDC_HAND))
		.SetFontUnderline(FALSE)
		.SetActualText("http://www.ping-systems.com" );
		

	m_PersonalHomePage.SetLink(TRUE)
		.SetTextColor(RGB(0,0,255))
		.SetLinkCursor(AfxGetApp()->LoadCursor(IDC_HAND))
		.SetFontUnderline(FALSE)
		.SetActualText("http://kumarv.tripod.com" );


	m_MailTo.SetLink(TRUE)
		.SetTextColor(RGB(0,0,255))
		.SetLinkCursor(AfxGetApp()->LoadCursor(IDC_HAND))
		.SetActualText( "mailto:kumar__v@hotmail.com&subject=Version " + theApp.m_szVersion )
		.SetFontUnderline(FALSE);

	m_CommercialLicense.SetLink(TRUE)
		.SetTextColor(RGB(0,0,255))
		.SetFontUnderline(TRUE)
		.SetLinkCursor(AfxGetApp()->LoadCursor(IDC_HAND))
		.SetActualText( 
			"mailto:kumar__v@hotmail.com&subject=Version " 
			+ theApp.m_szVersion 
			+ " - Commercial License " )
		.SetFontUnderline(FALSE)
		.SetFontBold(TRUE);

	// TODO: Add extra initialization here
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CAboutDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

    CSplashWnd::EnableSplashScreen(TRUE,FALSE);
	CSplashWnd::ShowSplashScreen(this);
	//CDialog::OnLButtonDblClk(nFlags, point);
}

BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (CSplashWnd::PreTranslateAppMessage(pMsg))
		return TRUE;
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CMy8085App::Help(int wParam, int lParam)
{
    //return;
    static BOOL bHelpCalled=FALSE;

    if(bHelpCalled)
        return ;
    bHelpCalled=TRUE;
    char startDir[_MAX_PATH+1];
    char help[_MAX_PATH+1];
    GetModuleFileName(NULL,startDir,_MAX_PATH);

    // trace backward till the name of the executable file
    // starts
	int i=0;
    for(i=strlen(startDir); i>0 && startDir[i]!='\\'; i--)
                ;
    startDir[i]=0;
    sprintf(help,"%s\\Help\\index.html",startDir);
	int x;
    if((x=(UINT)::GotoURL(help,1))<=32)
    {
        char str[1024];
        sprintf(str,"Could not open <%s>",help);
        ::ShowErrorMessage(str);
    }
    Help();
    bHelpCalled=FALSE;
}

